<template>
  <div class="codeParseRule">
    <v-tableTitle
      :btnConfig="btnConfig"
      :title="$t('transmit.parserule')"
      :tableSelectData="selectData"
      @newCreate="newCreate"
      @parse="parse"
      @copy="copy"
      @del="del"
    >
    </v-tableTitle>
    <GTable
      ref="table"
      :hasCheck="true"
      :hasEdit="true"
      :otherHeight="0"
      :columnConfig="columnConfig"
      api="baBarcodeReadRule/queryList"
      @selection-change="handleSelectionChange"
      @setdialog="setdialog"
    >
    </GTable>
    <GDrawer
      :config.sync="dialogModule"
      @confirm="handleConfirm"
      v-loading="drawerLoading"
    >
      <component
        :ref="dialogModule.invalid"
        :invalid="dialogModule.invalid"
        :is="getCurrentComponent(dialogModule.invalid)"
      ></component>
    </GDrawer>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
    name: "codeParseRule",
    mixins: [baseMixin, config],
    data() {
        return {
          columnConfig:{ menu_id: 990, menu_code:"codeParseRule", menu_list_code:"codeParseRule", },
          dialogModule:{
            visible:false,
            title:"",
            invalid:"",
            btns:[]
          },
          drawerLoading:false,
          selectData:[]
        }
    },
    watch:{
      '$route':{
        handler(nv){
          this.dialogModule = { visible:false, title:"", invalid:"", btns:[] }
        }
      }
    },
    computed: {
      btnConfig() {
        return [
          { label: this.$t('common.create'), certigier: !this.btnPermNameList.includes('EN02070501'), invalid: "newCreate",component:() => import('./details/Detail.vue') },
          { label: this.$t('transmit.parse'), certigier: !this.btnPermNameList.includes('EN02070502'),disabled:true, invalid: "parse",component:() => import('./details/Detail.vue')  },
          { label: this.$t('common.copy'), certigier: !this.btnPermNameList.includes('EN02070503'),disabled:true, invalid: "copy",component:() => import('./details/Detail.vue')  },
          { label: this.$t('common.delete'), certigier: !this.btnPermNameList.includes('EN02070504'),disabled:true, invalid: "del" },
          { label: this.$t('common.detail'), certigier: true, invalid: "detail",component:() => import('./details/Detail.vue')  },
        ];
      },
      drawerBtns(){
        return [{label: this.$t('common.save'),type:"primary",invalid:"confirm"},{label: this.$t('common.cancel'),invalid:"cancel"}]
      }
    },
    methods: {
      handleConfirm(){
        if(this.$refs[this.dialogModule.invalid]){
          this.$refs[this.dialogModule.invalid].submit().then(res => {
            this.dialogModule = {visible:false, title:"", invalid:"", btns:this.drawerBtns};
            this.$refs.table.getTableData()
          }).catch(err => {
            console.log(err);
          })
        }
      },
      getCurrentComponent(invalid){
        return this.btnConfig.find(btn => btn.invalid == invalid)?.component;
      },
      handleSelectionChange(data){
        this.selectData = data
      },
      newCreate(){
        this.$store.commit("setRouteData", {readRuleType:'12301'});
        this.dialogModule.title = this.$t('transmit.createparserule');
        this.dialogModule.invalid = "newCreate";
        this.dialogModule.visible = true;
        this.dialogModule.btns = this.drawerBtns;
      },
      setdialog(data){
        this.$store.commit("setRouteData", data);
        this.dialogModule.title = this.$t('transmit.parseruledetail1')
        this.dialogModule.invalid = "detail";
        this.dialogModule.visible = true;
        this.dialogModule.btns = this.drawerBtns;
      },
      parse(data){
        this.$store.commit("setRouteData", data[0]);
        this.dialogModule.title = this.$t('transmit.parseruledetail1')
        this.dialogModule.invalid = "parse";
        this.dialogModule.visible = true;
        this.dialogModule.btns = []
      },
      copy(data){
        this.$store.commit("setRouteData", data[0]);
        this.dialogModule.title = this.$t('transmit.createparserule')
        this.dialogModule.invalid = "copy";
        this.dialogModule.visible = true;
        this.dialogModule.btns = this.drawerBtns;
      },
      del(data){
        if(data.length > 0){
          if(data.some(item => ['已失效','生效中'].includes(item.validStatus))){
            return this.$message('已失效、生效中的规则不允许删除')
          }
          const params = data.map(item => {return {'id':item.id}});
          this.$post("/baBarcodeReadRule/delete",params).then(res => {
            
              this.$message.success(this.$t('app.delMes'));
              this.$refs.table.getTableData()
          })
        }
      }
    },
    mounted() {
        this.$store.commit("setSkip", {});
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setTemporary", "");
        this.$store.commit("setRouteData", {});
    },
    components: {
        "v-tableTitle": tableTitle,
    }
};
</script>

<style scoped>
#querybuilder {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
